#include <iostream>
using std::cout;
using std::endl;

class Point{
public:
    Point(int x, int y)
    :_x(x)
    ,_y(y)
    {
        cout << "Point(int,int)" << endl;
    }

    void print(){
        cout << _x << "," << _y << endl;
    }

    ~Point(){
        cout << "~Point()" << endl;
    }

    //编译器默认会提供operator new/delete库函数
    void * operator new(size_t size){
        cout << "operator new:" << size << endl;
        return malloc(size);
    }

    //operator delete库函数---回收空间
    //当我们执行delete p时，实际上这个p便会传递给当前库函数
    void operator delete(void * p){
        cout << "operator delete" << endl;
        free(p);
    }


private:
    int _x;
    int _y;
};


void test(){
    //cout << sizeof(Point) << endl;
    //1.执行new表达式会调用operator new库函数------》封装调用malloc
    Point * pt = new Point(1, 2);
    pt->print();
    delete pt;
}

int main()
{
    test();
    return 0;
}

